package app.view.component.moment 
{
	import com.hutra.utils.StringHelper;
	import flash.utils.Dictionary;
	import app.model.locale.English;
	import app.model.locale.Vietnamese;
	/**
	 * Language
	 * Lang.a.locale = 'en'
	 * Lang.a.getDictionary('en') as Dictionary
	 * Lang.a.fillter(Locale, Dictionary) as void
	 * Class process localization as i18n on JAVA
	 * In computing, internationalization and localization (other correct spellings are internationalisation and localisation) are means of adapting computer software to different languages, regional differences and technical requirements of a target market. Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting internationalized software for a specific region or language by adding locale-specific components and translating text.
	 * The terms are frequently abbreviated to the numeronyms i18n (where 18 stands for the number of letters between the first i and last n in internationalization, a usage coined at DEC in the 1970s or 80s)[1] and L10n respectively, due to the length of the words. The capital L in L10n helps to distinguish it from the lowercase i in i18n[citation needed].
	 * Some companies, like IBM and Sun Microsystems, use the term "globalization" for the combination of internationalization and localization.[2]
	 * Microsoft[3] defines Internationalization as a combination of World-Readiness and localization. World-Readiness is a developer task, which enables a product to be used with multiple scripts and cultures (globalization) and separating user interface resources in a localizable format (localizability, abbreviated to L12y).[4]
	 * This concept is also known as NLS (National Language Support or Native Language Support).
	 * This's funny :D
	 * I+n1t2e3r4n5a6t7i8o9n10a11l12i13z14a15t16i17o18+n
	 * @author hungconcon
	 */
	public class Lang extends Object {		
		/**
		 * Singleton determine the object initialization .
		 */
		private static var lang:Lang;
		/**
		 * Determining variable contains the language
		 */
		private var dic:Dictionary;
		/**
		 * language locale, defaults to Vietnamese
		 */
		public var locale:String = 'vi';
		/**
		 * Create a constuctor with arguments object is an array language , language n be set in the array
		 * language format is a binary file that is attached to the game and regardless of the language through class
		 * @see function a()
		 * @see function fillter()
		 * @param	a as 
		 * @private class list
		 */
		public function Lang(a:Array) {
			dic = new Dictionary(); 
			for (var i:int = 0; i < a.length; i++ ) 
				fillter(a[i].a, a[i].b);
		}
		/**
		 * Create a singleton with the argument : language code , binary block language
		 * locale language used to define the language
		 * restore the language is a binary file format each language including key line and the same value as in java
		 */
		public static function get a():Lang {
            if (!lang) {
				lang = new Lang([
									{a:'vi', b:split(new Vietnamese().toString()) },
									{a:'en', b:split(new English().toString()) }
								]);
			}
            return lang;
        }
		/**
		 * Get the value of a line in the file i18n , is the key argument
		 * The default locale is a predefined " locale language " , ex : en , en , cn , ge
		 * @param	key
		 * @return returns a string value
		 */
		public function getValue(key:String):String {
			if (locale == '') 
				return 'Language Error!';
			return dic[locale][key];
		}
		/**
		 * Language code , the value used to define the language
		 * @param	key
		 * @return
		 */
		public function getDictionary(key:String):Dictionary {
			locale = key;
			return dic[key];
		}
		/**
		 * add the values ​​in the binary file into memory
		 * ex: data.word=hello
		 * @param	key
		 * @param	value
		 */		
		protected function fillter(key:String, value:Dictionary):void { 
			dic[key] = value;
		}
		/**
		 * Treats each line in the language file , cut it up into each object , the object is moved to the
		 * dictionary memory card with the key and value, key to define the value
		 * @param	value
		 * @return
		 */
		protected static function split(value:String):Dictionary{
			var d:Dictionary = new Dictionary();
			var s:String = '';
			var l:Array = value.split('  \n                    ');
			for (var i:int = 0; i < l.length; i++) {
				s =  StringHelper.trim(l[i],' ');
				if(s != ''){
					var k:String = s.substring(0, s.indexOf('='));
					var v:String = (s.indexOf('=')!=-1)?(s.substring(s.indexOf('=') + 1, s.length)):'';
					d[k] = v;
				}
			}
			return d;
		}
	}
}